// pages/good-search/good-search.js
const util = require('../../utils/util.js');
import api from '../../utils/request/api'
import { searchHistory } from '../../utils/request/apiurl'

Page({
	data: {
		// tasteList: [{
		// 		id: 1,
		// 		name: '酸牛奶',
		// 		current: true
		// 	},
		// 	{
		// 		id: 2,
		// 		name: '酸牛奶',
		// 		current: false
		// 	},
		// 	{
		// 		id: 3,
		// 		name: '纯牛奶',
		// 		current: false
		// 	}
		// ],
		tasteList:[],
		inputShowed: false,
		inputVal: "",
		searchGood: {
			page: 1,
			size: 20,
			pages:0
		},
		isRequest: false,
		uid:''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var userInfo = wx.getStorageSync('userInfo');
		if (userInfo.rank) {
			this.setData({
				uid:userInfo.uid,
			})
		}
		this.searchHistory()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		// this.searchHistory()
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.setData({
			tasteList:[]
		})
		this.searchHistory()
		setTimeout(function () {
		wx.hideNavigationBarLoading() //完成停止加载	
		wx.stopPullDownRefresh() //停止下拉刷新
		
		}, 1500);},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {
		if (!this.data.isRequest && this.data.searchGood.page < this.data.searchGood.pages) {
			this.setData({
			  searchGood: {
			    page: this.data.searchGood.page + 1,
			    size: 10,
			    pages: this.data.searchGood.pages
			  }
			})
			this.handleComment()
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	showInput: function () {
	    this.setData({
	        inputShowed: true
	    });
	},
	hideInput: function () {
	    this.setData({
	        inputVal: "",
	        inputShowed: false
	    });
	},
	clearInput: function () {
	    this.setData({
	        inputVal: ""
	    });
	},
	inputTyping: function (e) {
	    this.setData({
	        inputVal: e.detail.value
	    });
	},
	// 历史记录列表
	searchHistory(){
		var source = {
			uid: this.data.uid,
		}
		api.post(searchHistory.list, source).then(res => {
			var tasteList=[]
			for(var i=0;i<res.length;i++){
				// console.log(res[i])
				if(res[i]!='undefined'){
					// console.log(res[i],'res[i]res[i]')
					var obj={}
					obj['id']=i;
					obj['name']=res[i];
					obj['current']=false;
					tasteList.push(obj)
				}	
			}
			// 数组去重
			var obj = {};
			tasteList = tasteList.reduce(function(item, next) {	
				// console.log(item,next)
			  obj[next.name] ? '' : obj[next.name] = true && item.push(next);			
			  return item;			
			   }, []);
			// console.log(tasteList)
			this.setData({
				tasteList:tasteList
			})
			this.setData({
				'tasteList[0].current':true
			})
			// distinct(this.data.tasteList)
		}).catch(err => {
			// console.log(err)
			// wx.showToast({
			// 	title: err.message,
			// 	icon: 'none'
			// })
		})
	},
	//失去焦点搜索
	blurSearch(e){
		// console.log(this.data.inputVal)
		if(this.data.inputVal.length > 0){
			wx.redirectTo({ url: '../search-result/search-result?id='+this.data.inputVal })
			// console.log(e.detail.value)
		}
		
	},
	// 历史记录搜索
	Search(e){
		// console.log(e)
		if(this.data.tasteList.length>0){
			// console.log(e.target.dataset.id)
			wx.redirectTo({ url: '../search-result/search-result?id='+e.currentTarget.dataset.name })
		}	
	},
	// 清除历史记录
	handDel(){
		if(wx.getStorageSync('userInfo')!=''){
			api.post(searchHistory.deleteUserKey, {uid:this.data.uid||wx.getStorageSync('userInfo').uid}).then(res => {
				this.setData({
					tasteList:[]
				})
				wx.showToast({
					title:'删除成功',
					icon: 'none',
					duration: 2000
				})
			}).catch(err => {
				// console.log(err)
				wx.showToast({
					title: err.message,
					icon: 'none'
				})
			})
		}else{
			wx.showToast({
				title: '暂未登录，请前去登录',
				icon: 'none',
				duration: 2000
			})
		}
		
		
	},

})
